<?php

/**

 * Created by PhpStorm.

 * User: win7

 * Date: 2018/7/16 0016

 * Time: 17:02

 */



namespace Home\Controller;





class NewsController extends CommonController

{

    //列表页

    public function index(){

        $page = $_GET['p'] ? $_GET['p']:1;

        if(isset($_REQUEST['keyword'])){

            $keyword = explode(' ',$_REQUEST['keyword']);

            $where = '';

            $keyword = array_filter($keyword);//去空

            $keyword = array_unique($keyword);//去重



            foreach($keyword as $k => $v){

                if($k == 0){

                    $where .= "title like '%".$v."%'";

                }else{

                    $where .= " AND title like '%".$v."%'";

                }

            }



            $count = M("news")->where($where)->count();// 查询满足要求的总记录数

            $Page = new \Home\Model\PageModel;// 实例化分页类

            $show = $Page->createpage("http://zixun.xiangtws.com/Home/News/index/keyword/{$_REQUEST['keyword']}",$count,10,$page);// 分页



            $news_arr =M("news")->where($where)->order('addtime desc')->page($page.',10')->field('id,title,addtime')->select();//所有文章



            $this->assign('IS_keyword',true);

            $this->assign('keyword',$keyword);

        }else{

            $pid = $_GET['pid'];

            $arr_id = get_idchild($pid);//获取下级子分类



            //导航id处理

            $parentid = getParent($pid);//获取所有父分类

            sort($parentid);//排序



            $count = M("news")->where("pid = {$pid}")->count();// 查询满足要求的总记录数



            $Page = new \Home\Model\PageModel;// 实例化分页类

            $show = $Page->createpage("http://zixun.xiangtws.com/Home/News/index/pid/{$pid}",$count,10,$page);// 分页



            $news_arr =M("news")->where("pid = {$pid}")->order('addtime desc')->page($page.',10')->field('id,title,addtime')->select();//所有文章

        }//end if





        $this->assign("arr_id",$arr_id);

        $this->assign("parentid",$parentid);

        $this->assign("show",$show);

        $this->assign("news_arr",$news_arr);

        $this->assign("head_title",'乡土卫士资讯站');

        $this->assign("keywords",'乡土卫士资讯站');

        $this->assign("description",'乡土卫士资讯站');



        if(is_mobile()){

            // 广告图

            $banner = M('advert')->where("type=1 and place=0")->find();

            $this->assign("banner",$banner);

            $this->display('news-h5');

        }else{

            // 广告图

            $banner = M('advert')->where("type=1 and place=1")->find();

            $this->assign("banner",$banner);

            $this->display('news');

        }//end if

//        dump($keyword);

//        dump($show);

//        dump($news_arr);

    }//end fun

	//js触发点击
	public function jsclick(){
        $nid = $_GET['nid'];
        M("news")->where("id = {$nid}")->setInc('view',1);//js触发增加阅读量
	}//end fun

    //详情页

    public function info(){

        $nid = $_GET['nid'];

        if(!$nid){

            $this->error("访问错误");

        }//end if




            M("news")->where("id = {$nid}")->setInc('click',1);//增加阅读量click

            M("news")->where("id = {$nid}")->save(['addtime'=>time()]);//时间修改


        $news = M("news")->where("id = {$nid}")->find();//咨讯信息

		//伪原创SEO
		if($news['orseo']==0){
			$same_word=D('same_word');
			$wordarr=$same_word->select();
			foreach($wordarr as $i=>$one){
				$news['content']=str_replace($one["a_word"],$one["b_word"],$news['content'],$x);
				if($x==0)$news['content']=str_replace($one["b_word"],$one["a_word"],$news['content'],$y);
				//unset($wordarr[$i]);  //删除元素 
			}//end foreach
			//更新文章内容和SEO状态到数据库
			$seodata=array(); 
			$seodata["content"]=$news['content'];
			$seodata["orseo"]=1;//dump($seodata);
			M("news")->where("id = {$nid}")->save($seodata);
		}//end if

        //自动分类

        if($news['pid'] == 0){

            $cate_all = M("cate")->where("keywords != ''")->field('id,keywords')->select();

            foreach($cate_all as $k => $v){

                $cate_key = explode(',',$v['keywords']);//字符串转数组

                foreach ($cate_key as $ck => $cv){

                    if($cv != '' && strstr($news['title'],$cv) != ''){

                        M("news")->where("id = {$nid}")->save(['pid'=>$v['id']]);//分类pid修改

                        $news['pid'] = $v['id'];//分类pid修改

                        goto cateif_end;//退出if

                    }//end if

                }//end foreach

            }//end foreach

        }//end if

        cateif_end:



        //关键字处理

        if(empty($news['keywords'])){

            $keywords_all = M("keywords")->select();//所有关键字

            $keywords = '';

            foreach($keywords_all as $k => $v){

                if(strstr($news['content'],$v['keyword']) !=  ''){

                    if($keywords == ''){

                        $keywords .= $v['keyword'];

                    }else{

                        $keywords .= ','.$v['keyword'];

                    }//end if

                }//end if

            }//end foreach

            M("news")->where("id = {$nid}")->save(['keywords'=>$keywords]);

        }else{

            $keywords = $news['keywords'];//关键字

        }//end if



        //头部描述

        $description = preg_replace(array("(<[^>]*>)" ),"",$news['content']);//过滤html

        $description = mb_substr($description,0,200,"UTF-8");//截取字符



        //导航id处理

        $header = getParent($news['pid']);//获取所有父分类

        sort($header);//排序



        //上下篇内容

        $another['上一篇'] = M('news')->where("id < {$nid}")->order("id DESC")->getField('id');//上一篇

        $another['下一篇'] = M('news')->where("id > {$nid}")->order("id ASC")->getField('id');//下一篇

        foreach($another as $k => $v){

            if($v != NULL){

                $another[$k]= M("news")->where("id = {$v}")->field('id,title')->find();//咨讯信息

            }

        }//end foreach



        $this->assign("news",$news);

        $this->assign("header",$header);

        $this->assign("another",$another);

        $this->assign("head_title",'乡土卫士资讯站');

        $this->assign("keywords",$keywords);

        $this->assign("description",$description);

        if(is_mobile()){

            // 广告图

            $banner = M('advert')->where("type=2 and place=0")->find();

            $banner1 = M('advert')->where("type=3 and place=0")->find();

            $this->assign("banner",$banner);

            $this->assign("banner1",$banner1);

            $this->display('newsdetail-h5');

        }else{

            // 广告图

            $banner = M('advert')->where("type=2 and place=1")->find();

            $banner1 = M('advert')->where("type=3 and place=1")->find();

            $this->assign("banner",$banner);

            $this->assign("banner1",$banner1);

            $this->display('newsdetail');

        }//end if



//        dump($another);

//        dump($description);

    }//end fun

}//end class